package GUI;
import java.awt.*;
import java.awt.print.Paper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import 学生.Student;
import 数据库和网络.myDatebase;

//JFreeChart Line Chart（折线图）
public class overallRankChart {

    /**
     * 创建JFreeChart Line Chart（折线图）
     */
    public static void main(String[] args) {
        // 步骤1：创建CategoryDataset对象（准备数据）
        overallRankChart demo = new overallRankChart();
        demo.display(new Student());
    }

    // 根据CategoryDataset创建JFreeChart对象
    public void display(Student curr){
        CategoryDataset dataset = createDataset(curr);

        // 步骤2：根据Dataset 生成JFreeChart对象，以及做相应的设置
        JFreeChart freeChart = createChart(dataset);
        ChartFrame chartFrame=new ChartFrame("历次成绩及排名",freeChart);
        chartFrame.pack(); //以合适的大小展现图形
        chartFrame.setLocationRelativeTo(null);
        chartFrame.setVisible(true);//图形是否可见
    }
    public static JFreeChart createChart(CategoryDataset categoryDataset) {
        // 创建JFreeChart对象：ChartFactory.createLineChart
        JFreeChart jfreechart = ChartFactory.createLineChart("历次成绩及排名", // 标题
                "考试", // categoryAxisLabel （category轴，横轴，X轴标签）
                "分数", // valueAxisLabel（value轴，纵轴，Y轴的标签）
                categoryDataset, // dataset
                PlotOrientation.VERTICAL, true, // legend
                false, // tooltips
                false); // URLs
        // 使用CategoryPlot设置各种参数。以下设置可以省略。
        CategoryPlot mPlot = (CategoryPlot)jfreechart.getPlot();
        mPlot.setBackgroundPaint(Color.LIGHT_GRAY);
        mPlot.setRangeGridlinePaint(Color.BLUE);//背景底部横虚线
        mPlot.setOutlinePaint(Color.darkGray);//边界线

        mPlot.setBackgroundAlpha(0.5f);
        // 前景色 透明度
        mPlot.setForegroundAlpha(0.5f);
        // 其他设置 参考 CategoryPlot类
        LineAndShapeRenderer renderer = (LineAndShapeRenderer)mPlot.getRenderer();
        renderer.setBaseShapesVisible(true); // series 点（即数据点）可见
        renderer.setBaseLinesVisible(true); // series 点（即数据点）间有连线可见
        //renderer.setUseSeriesOffset(true); // 设置偏移量
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        return jfreechart;
    }

    /**
     * 创建CategoryDataset对象
     *
     */

    public static CategoryDataset createDataset(){
        return new DefaultCategoryDataset();
    }

    public static CategoryDataset createDataset(Student currentstudent) {
        ArrayList<String> paperlist=new ArrayList<>();
        ArrayList<Integer>  ranklist=new ArrayList<>();
        myDatebase chartHelper=new myDatebase("result_record");
        myDatebase paperFinder=new myDatebase("papercollection");
        String sql="select * from papercollection."+currentstudent.getMyClass();
        try {
            paperFinder.statement.execute(sql);
            paperFinder.resultSet=paperFinder.statement.executeQuery(sql);

            while(paperFinder.resultSet.next()){
                paperlist.add(paperFinder.resultSet.getString("paperTitle"));
            }

            Iterator<String> ite=paperlist.iterator();

            while(ite.hasNext()){
                String current=ite.next();
                sql=String.format("select * from result_record.%s where result_record.%s.studentID=%s",
                        current,current,currentstudent.getSchoolID());
                chartHelper.statement.execute(sql);
                chartHelper.resultSet=chartHelper.statement.executeQuery(sql);
                while(chartHelper.resultSet.next()){
                    int sel=chartHelper.resultSet.getInt("selection_score");
                    int def=chartHelper.resultSet.getInt("definition_score");
                    int fill=chartHelper.resultSet.getInt("fillingblank_score");
                    int sub=chartHelper.resultSet.getInt("subjective_score");
                    ranklist.add(sel + def + fill + sub);
                }

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        chartHelper.close();
        paperFinder.close();

        DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
        for(int i=0;i<ranklist.size();i++){
            categoryDataset.addValue(ranklist.get(i),"成绩",paperlist.get(i));
        }
        categoryDataset.addValue(1,"排名",paperlist.get(0));
        for(int i=1;i<ranklist.size();i++){
            categoryDataset.addValue(0,"排名",paperlist.get(i));
        }
        return categoryDataset;
    }

}



